//
//  Details.m
//
//  Created by TuanNT on 5/25/14
//  Copyright (c) 2014 Fsoft. All rights reserved.
//

#import "Activity.h"


NSString *const kDetailsCustomer = @"Customer";
NSString *const kDetailsDescription = @"Description";
NSString *const kDetailsTime = @"Time";
NSString *const kDetailsTitle = @"Title";
NSString *const kDetailsLink = @"Link";
NSString *const kActivityDetailsImage = @"Image";


@interface Activity ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation Activity

@synthesize customer = _customer;
@synthesize detailsDescription = _detailsDescription;
@synthesize time = _time;
@synthesize title = _title;
@synthesize link = _link;
@synthesize image = _image;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
	return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
	self = [super init];

	// This check serves to make sure that a non-NSDictionary object
	// passed into the model class doesn't break the parsing.
	if (self && [dict isKindOfClass:[NSDictionary class]]) {
		self.customer = [self objectOrNilForKey:kDetailsCustomer fromDictionary:dict];
		self.detailsDescription = [self objectOrNilForKey:kDetailsDescription fromDictionary:dict];
		self.time = [self objectOrNilForKey:kDetailsTime fromDictionary:dict];
		self.title = [self objectOrNilForKey:kDetailsTitle fromDictionary:dict];
		self.link = [self objectOrNilForKey:kDetailsLink fromDictionary:dict];
		self.image = [self objectOrNilForKey:kActivityDetailsImage fromDictionary:dict];
	}

	return self;
}

- (NSDictionary *)dictionaryRepresentation {
	NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
	[mutableDict setValue:self.customer forKey:kDetailsCustomer];
	[mutableDict setValue:self.detailsDescription forKey:kDetailsDescription];
	[mutableDict setValue:self.time forKey:kDetailsTime];
	[mutableDict setValue:self.title forKey:kDetailsTitle];
	[mutableDict setValue:self.link forKey:kDetailsLink];
	[mutableDict setValue:self.image forKey:kActivityDetailsImage];

	return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description {
	return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
	id object = [dict objectForKey:aKey];
	return [object isEqual:[NSNull null]] ? nil : object;
}

#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder {
	self = [super init];

	self.customer = [aDecoder decodeObjectForKey:kDetailsCustomer];
	self.detailsDescription = [aDecoder decodeObjectForKey:kDetailsDescription];
	self.time = [aDecoder decodeObjectForKey:kDetailsTime];
	self.title = [aDecoder decodeObjectForKey:kDetailsTitle];
	self.link = [aDecoder decodeObjectForKey:kDetailsLink];
	self.image = [aDecoder decodeObjectForKey:kActivityDetailsImage];
	return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
	[aCoder encodeObject:_customer forKey:kDetailsCustomer];
	[aCoder encodeObject:_detailsDescription forKey:kDetailsDescription];
	[aCoder encodeObject:_time forKey:kDetailsTime];
	[aCoder encodeObject:_title forKey:kDetailsTitle];
	[aCoder encodeObject:_link forKey:kDetailsLink];
	[aCoder encodeObject:_image forKey:kActivityDetailsImage];
}

- (id)copyWithZone:(NSZone *)zone {
	Activity *copy = [[Activity alloc] init];

	if (copy) {
		copy.customer = [self.customer copyWithZone:zone];
		copy.detailsDescription = [self.detailsDescription copyWithZone:zone];
		copy.time = [self.time copyWithZone:zone];
		copy.title = [self.title copyWithZone:zone];
		copy.link = [self.link copyWithZone:zone];
		copy.image = [self.image copyWithZone:zone];
	}

	return copy;
}

@end
